#ifdef WIN32
#include <windows.h>
#endif

#include <time.h>
#include "CVanaTime.h"
#include "Utils/CWindowClass.h"
#include "CConsole.h"

CVanaTime* CVanaTime::_instance = NULL;

CVanaTime * CVanaTime::getInstance() 
{
	if(_instance == NULL) {
		_instance = new CVanaTime();
	}
	return _instance;
}

CVanaTime::CVanaTime()
{
	m_customOffset = 0;
	SyncTime();
}

void CVanaTime::setCustomOffset(unsigned int newOffset)
{
	m_customOffset = newOffset;
	SyncTime();
	//SyncClients(); //if possible ...
}

unsigned long long CVanaTime::getDate(void)
{
	SyncTime();
	return m_vanaDate;
}

unsigned int CVanaTime::getYear(void)
{
	SyncTime();
	return m_vYear;
}

unsigned int CVanaTime::getMonth(void)
{
	SyncTime();
	return m_vMon;
}

unsigned int CVanaTime::getDayOfTheMonth(void)
{
	SyncTime();
	return m_vDate;
}

unsigned int CVanaTime::getHour(void)
{
	SyncTime();
	return m_vHour;
}

unsigned int CVanaTime::getMinute(void)
{
	SyncTime();
	return m_vMin;
}

unsigned int CVanaTime::getWeekday(void)
{
	SyncTime();
	return m_vDay;
}

void CVanaTime::SyncTime(void) 
{
	time_t rawtime;
	time(&rawtime);
	unsigned long long convertedTime;

	//Convert real time into Vana minutes
	convertedTime = (int)(((unsigned long long)rawtime - (unsigned long long)baseDate) / 2.4);
	convertedTime += m_customOffset;
	convertedTime += ((898 * 360 + 30) * (unsigned long long)msGameDay);

	this->m_vYear = (int)(convertedTime / msGameYear);
	this->m_vMon  = (int)((convertedTime / msGameMonth) % 12)+ 1;
	this->m_vDate = (int)((convertedTime % msGameMonth) / msGameDay) + 1;
	this->m_vHour = (int)((convertedTime % msGameDay) / msGameHour);
	this->m_vMin  = (int)(convertedTime % msGameHour);
	this->m_vDay  = (int)((convertedTime % msGameWeek) / msGameDay);

	//Add starting date to converted time
	if (convertedTime!=m_vanaDate)
	{
		//Update Time in the statistic window
		CConsole::getOutputWindow()->setValue("Vana-Daytime		","%d:%d",m_vHour,m_vMin);
		CConsole::getOutputWindow()->setValue("Vana-Date		","%d.%d.%d",m_vDate,m_vMon,m_vYear);
		CConsole::getOutputWindow()->setValue("Vana-Weekday		","%d",m_vDay);
		CConsole::getOutputWindow()->setValue("Vana-Date(absolute)	","%d",convertedTime);
	}
	m_vanaDate =  convertedTime;

	

}

void CVanaTime::getTime(unsigned long long* vanaDate, unsigned int* year, unsigned int* month, unsigned int* dayOfTheMonth, unsigned int* hour, unsigned int* minute, unsigned int* weekday)
{
	SyncTime();
	if (vanaDate!=NULL)
		*vanaDate = m_vanaDate;
	if (year!=NULL)
		*year = m_vYear;
	if (month!=NULL)
		*month = m_vMon;
	if (dayOfTheMonth!=NULL)
		*dayOfTheMonth = m_vDate;
	if (hour!=NULL)
		*hour = m_vHour;
	if (minute!=NULL)
		*minute = m_vMin;
	if (weekday!=NULL)
		*weekday = m_vDay;
}

bool CVanaTime::isShopUpdateTime() 
{
	SyncTime();
	return ((m_vHour == 0) && (m_vMin == 0));
}

bool CVanaTime::isUndeadTime() 
{
	SyncTime();
	if((this->m_vHour < 4) || (this->m_vHour >= 20))
		return true;
	else
		return false;

}

bool CVanaTime::isBatTime() 
{
	SyncTime();
	if((this->m_vHour < 6) || (this->m_vHour >= 18))
		return true;
	else
		return false;

}